Page({
  data: {
    currentPlan: 'week',
    courseDictProgress: 35,    // 课程词库总进度
    cambridgeDictProgress: 25, // 剑桥词库总进度
    planConfig: {
      week: {
        count: 3,
        color: '#4285f4'  // 蓝色
      },
      month: {
        count: 12,
        color: '#34a853'  // 绿色
      },
      quarter: {
        count: 36,
        color: '#fbbc05'  // 黄色
      },
      semester: {
        count: 72,
        color: '#ea4335'  // 红色
      }
    },
    // 课程词库数据
    courseDict: {
      grade3: {
        name: '三年级',
        expanded: false,
        progress: 0,
        lessons: Array(8).fill().map((_, i) => ({
          id: i + 1,
          name: `第${i + 1}课`,
          completed: i < 2
        }))
      },
      grade4: {
        name: '四年级',
        expanded: false,
        progress: 0,
        lessons: Array(8).fill().map((_, i) => ({
          id: i + 1,
          name: `第${i + 1}课`,
          completed: false
        }))
      },
      grade5: {
        name: '五年级',
        expanded: false,
        progress: 0,
        lessons: Array(8).fill().map((_, i) => ({
          id: i + 1,
          name: `第${i + 1}课`,
          completed: false
        }))
      }
    },
    // 剑桥词库数据
    cambridgeDict: {
      starter: {
        name: '预备级',
        expanded: false,
        progress: 0,
        semesters: {
          1: Array(16).fill().map((_, i) => ({
            id: i + 1,
            name: `第${i + 1}单元`,
            completed: i < 2
          })),
          2: Array(16).fill().map((_, i) => ({
            id: i + 1,
            name: `第${i + 1}单元`,
            completed: false
          }))
        }
      },
      level1: {
        name: '剑桥一级',
        expanded: false,
        progress: 0,
        semesters: {
          1: Array(16).fill().map((_, i) => ({
            id: i + 1,
            name: `第${i + 1}单元`,
            completed: false
          })),
          2: Array(16).fill().map((_, i) => ({
            id: i + 1,
            name: `第${i + 1}单元`,
            completed: false
          }))
        }
      },
      level2: {
        name: '剑桥二级',
        expanded: false,
        progress: 0,
        semesters: {
          1: Array(16).fill().map((_, i) => ({
            id: i + 1,
            name: `第${i + 1}单元`,
            completed: false
          })),
          2: Array(16).fill().map((_, i) => ({
            id: i + 1,
            name: `第${i + 1}单元`,
            completed: false
          }))
        }
      },
      level3: {
        name: '剑桥三级',
        expanded: false,
        progress: 0,
        semesters: {
          1: Array(16).fill().map((_, i) => ({
            id: i + 1,
            name: `第${i + 1}单元`,
            completed: false
          })),
          2: Array(16).fill().map((_, i) => ({
            id: i + 1,
            name: `第${i + 1}单元`,
            completed: false
          }))
        }
      }
    }
  },

  // 计算课程词库进度
  calculateCourseDictProgress() {
    let totalLessons = 0;
    let completedLessons = 0;

    Object.values(this.data.courseDict).forEach(grade => {
      grade.lessons.forEach(lesson => {
        totalLessons++;
        if (lesson.completed) completedLessons++;
      });
    });

    const progress = Math.round((completedLessons / totalLessons) * 100);
    this.setData({ courseDictProgress: progress });
  },

  // 计算剑桥词库进度
  calculateCambridgeDictProgress() {
    let totalUnits = 0;
    let completedUnits = 0;

    Object.values(this.data.cambridgeDict).forEach(level => {
      Object.values(level.semesters).forEach(semester => {
        semester.forEach(unit => {
          totalUnits++;
          if (unit.completed) completedUnits++;
        });
      });
    });

    const progress = Math.round((completedUnits / totalUnits) * 100);
    this.setData({ cambridgeDictProgress: progress });
  },

  // 获取计划标记
  getPlanMark(index, type) {
    const config = this.data.planConfig[this.data.currentPlan];
    if (index < config.count) {
      return {
        marked: true,
        color: config.color
      };
    }
    return {
      marked: false,
      color: ''
    };
  },

  // 修改原有的数据处理方法
  calculateGradeProgress() {
    // 计算课程词库每个年级的进度
    Object.keys(this.data.courseDict).forEach(grade => {
      const lessons = this.data.courseDict[grade].lessons;
      // 计算完成进度
      const completed = lessons.filter(lesson => lesson.completed).length;
      const progress = Math.round((completed / lessons.length) * 100);

      const updatedLessons = lessons.map((lesson, index) => {
        const mark = this.getPlanMark(index, 'course');
        return {
          ...lesson,
          marked: mark.marked,
          markColor: mark.color
        };
      });

      // 使用一个对象来更新多个数据
      const updateData = {
        [`courseDict.${grade}.lessons`]: updatedLessons,
        [`courseDict.${grade}.progress`]: progress
      };
      this.setData(updateData);
    });

    // 计算剑桥词库每个级别的进度
    Object.keys(this.data.cambridgeDict).forEach(level => {
      Object.entries(this.data.cambridgeDict[level].semesters).forEach(([semester, units]) => {
        // 计算完成进度
        const completed = units.filter(unit => unit.completed).length;
        const progress = Math.round((completed / units.length) * 100);

        const updatedUnits = units.map((unit, index) => {
          const mark = this.getPlanMark(index, 'cambridge');
          return {
            ...unit,
            marked: mark.marked,
            markColor: mark.color
          };
        });

        // 使用一个对象来更新多个数据
        const updateData = {
          [`cambridgeDict.${level}.semesters.${semester}`]: updatedUnits,
          [`cambridgeDict.${level}.progress`]: progress
        };
        this.setData(updateData);
      });
    });
  },

  onLoad() {
    // 计算初始进度
    this.calculateCourseDictProgress();
    this.calculateCambridgeDictProgress();
    this.calculateGradeProgress();
  },

  // 更新进度时同时更新子目录进度
  updateProgress(type) {
    if (type === 'course') {
      this.calculateCourseDictProgress();
      this.calculateGradeProgress();
    } else if (type === 'cambridge') {
      this.calculateCambridgeDictProgress();
      this.calculateGradeProgress();
    }
  },

  // 切换年级/级别的展开状态
  toggleGrade(e) {
    const { grade } = e.currentTarget.dataset;
    if (grade.startsWith('grade')) {
      // 课程词库
      const key = `courseDict.${grade}.expanded`;
      this.setData({
        [key]: !this.data.courseDict[grade].expanded
      });
    } else {
      // 剑桥词库
      const key = `cambridgeDict.${grade}.expanded`;
      this.setData({
        [key]: !this.data.cambridgeDict[grade].expanded
      });
    }
  },

  // 选择课程
  selectLesson(e) {
    const { grade, lesson } = e.currentTarget.dataset;
    console.log(`选择了${this.data.courseDict[grade].name}的第${lesson}课`);
    // TODO: 处理课程选择逻辑
  },

  // 选择单元
  selectUnit(e) {
    const { level, semester, unit } = e.currentTarget.dataset;
    console.log(`选择了${this.data.cambridgeDict[level].name}${semester === 1 ? '上' : '下'}册的第${unit}单元`);
    // TODO: 处理单元选择逻辑
  },

  // 切换计划类型
  switchPlan(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      currentPlan: type
    }, () => {
      // 切换计划类型后重新计算标记
      this.calculateGradeProgress();
    });
  }
}); 